<?php
class Integral_logAction extends IntesysAction {
	function index(){  
		$integral_logDB=D('Integral_log');
        $integraltypeDB=D('Integral_type');
		$map['formula']=array('neq','');
		$map['site_id']=__SITEID__;
        $this->taskform = M('Form') -> where($map) -> select();
		//筛选
		$this->initView();
		$map=$integral_logDB->filter();
		$pagesize = 20;
		if(isset($_GET['pagesize'])){
			$pagesize = intval($_GET['pagesize']);
		}
		
        if($this->_param('keywords')){
            $keywords=trim($this->_param('keywords'));
            $where['m.company_id']        =array('like','%'.$keywords.'%');
            $where['r.membername']        =array('like','%'.$keywords.'%');
            $where['r.memberpositionname']=array('like','%'.$keywords.'%');
            $where['r.remark']            =array('like','%'.$keywords.'%');
            $where['r.integralname']      =array('like','%'.$keywords.'%');
            $where['r.approvername']      =array('like','%'.$keywords.'%');
            $where['r.changenum']         =array('like','%'.$keywords.'%');
            $where['_logic']='or';
            $map['_complex'] = $where;
        }
        $join ='__MEMBER__ m on m.id =r.member_id';
        $field="m.company_id as member_id,r.membername as membername,r.time as time,r.memberpositionname as memberpositionname,r.remark as remark,r.integralname as integralname,r.changenum as changenum,r.approvername as approvername,r.id as id";
		$this->pages=$integral_logDB->_list($this->view,$join,$field,$map,'r',$pagesize);
		//获取 筛选或添加 所需的积分规则类型，积分类型，部门，所有员工等数据
        $this->assign('grouplist',D('Member_position')->where(['site_id'=>__SITEID__,'status'=>1])->select());
        $this->assign('memberlist',D('Member')->getallgroupmember2());
	    $this->assign('ruletypelist',D('Integral_ruletype')->getList());
	    $this->assign('integraltypelist',$integraltypeDB->getType());
	    $this->assign('integraltypearr',$integraltypeDB->getTypeField('id,remark'));
		$this->assign('totalnum',$integral_logDB->getTotalnum($join,$map));
	    $this->display();
	}
	public function insert(){
        if(IS_POST){
            $_POST['site_id'] = (int)__SITEID__;
            $_POST['time']=strtotime($_POST['date']);
            $_POST['giver_id']=session(C('USER_AUTH_KEY'));
            $type=$_POST['typeid'];
            import("@.ORG.Integrals.".$type);
            $class=new $type(session(C('USER_AUTH_KEY')));
            $data='';
            if (!$this->dao->create($data)){$this->error($this->dao->getError());}
            //循环遍历所有员工添加数据
            $memberid=explode(",",$_POST['memberid']);
            $remark=isset($_POST['remark'])?$_POST['remark']:'';
            
            foreach($memberid as $val){
                if(trim($val)==''){continue;}
                $_POST['member_id']=(int)$val;
                if(!$class->giveIntegralMore($_POST['changenum'],$_POST['ruleid'],$_POST['member_id'],$remark,$_POST['rewarder_id'],$_POST['time'],$_POST['site_id'])){
                    $this->error($this->dao->getError());   
                };
            } 
            if(isset($_POST['requestType'])&&$_POST['requestType']==10001101){
                die(json_encode(array('statu'=>1)));
            }else{
                $this->success('操作成功', U('Intesys/' . MODULE_NAME . '/index')); 
            }
        }
	}
	public function update(){
		if($this->_post('id')){
			$memberDB=M('Member');
            $data['id']=$this->_post('id');
            //$typeid=M('integral_log')->where(array('id'=>$this->_get('id')))->getField('type_id');
            //$type=M('Integral_type')->where(array('id'=>$typeid))->getField('typename');
			if($this->_post('member_id')){
				$data['member_id']=$this->_post('member_id');
				$data['membername']=$this->_post('member_name');
				$data['memberpositionid']=$memberDB->where(['id'=>$data['member_id']])->getField('memberpositionid');
				$data['memberpositionname']=$memberDB->where(['id'=>$data['member_id']])->getField('memberpositionname');
			}else{
                $data['member_id']=$this->dao->where('id ='.$data['id'])->getfield('member_id');
            }
            $data['time']=strtotime($this->_post('time'));
			$data['remark']=$this->_post('remark');
			$data['changenum']=$this->_post('changenum');
			$data['giver_id']=$this->memberInfo['id'];
			$data['approvername']=$this->memberInfo['name'];
            /*import("@.ORG.Integrals.".$type);
            $class=new $type(session(C('USER_AUTH_KEY')));
            if($class->setIntegral($_POST)){
                $this->success('操作成功', U('Intesys/' . MODULE_NAME . '/index')); 
            }else{
                $this->error('参数错误');    
            }*/
            $data['logid']=$data['id'];
			$re=$this->dao->update($data);
            if($re){
                $this->success('更新成功');
            }else{
                $this->error('更新失败');
            }
		}
	}
	public function delete(){
		if($this->_get('id')){
            $typeid=M('integral_log')->where(array('id'=>$this->_get('id')))->getField('type_id');
            $type=M('Integral_type')->where(array('id'=>$typeid))->getField('typename');
            import("@.ORG.Integrals.".$type);
            $class=new $type(session(C('USER_AUTH_KEY')));
            if($class->deleteIntegral($this->_get('id'))){
                $this->success('操作成功', U('Intesys/' . MODULE_NAME . '/index')); 
            }else{
                $this->error('参数错误');    
            }
			//parent::delete();
		}
	}
    public function change(){
        $map['formula']=array('neq','');
        $this->taskform = M('Form') -> where($map) -> select();
		//$this->formcategory=D('Formcategory')->where(['site_id'=>__SITEID__,'status'=>1])->select();
		$this->assign('formcategorylist',D('Formcategory')->getList());
        $this->display();
    }
	//主页积分显示模块
	public function indexModule(){
		$inte=unserialize($this->userInfo['integralconfig']);
		$this->Binte=$inte['Binte'];
		$firstDay=strtotime(date('Y-m-01 00:00:00',time()));
		$lastDay=strtotime(date('Y-m-d 23:59:59',strtotime(date('Y-m-01 00:00:00',time()))).' +1 month -1 day');
		$map='time >'.$firstDay.' and time <'.$lastDay.' and site_id = '.__SITEID__.' and giver_id='.$this->UserInfo;
		$this->plusInte=$this->dao->where($map.' and changenum > 0')->sum('changenum');
		$this->lessInte=$this->dao->where($map.' and changenum < 0')->sum('changenum');
		$this->list=$this->dao->where(['site_id'=>__SITEID__,'member_id'=>$this->memberInfo['id']])->limit(10)->order('id desc')->select();
        $this->isLeader=getAccessMenu($this->accesslist, 'Intesys', 'Integral_log', 'indexModule');
		$this->display();	
	}
}

